package httpconnect;

import java.io.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * Created by zy on 2019/4/12.
 */
public class Book {
    private MenuBean menuBean;
    private ChapterBean chapterBean;

    Book() {
        menuBean = new MenuBean();
        chapterBean = new ChapterBean();
    }

    void start() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
        menuBean.responseData(getIndexHTML(), chapterBean);
    }

    private String getIndexHTML() throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
        String data = null;
        File file = new File(Const.index);
        if (!file.exists())
            data = HttpMethod.sendPost(Const.BaseUrl + /*"/books/14899/"*/"/45_45470", null, null, file);
        else {
            FileInputStream reader = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(reader, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            int end;
            while (true) {
                String temp = br.readLine();
                if (temp == null || temp.equals("")) {
                    break;
                }
                buffer.append(temp);
                System.out.println();
            }
            data = buffer.toString();
        }
        return data;
    }
}
